package com.hzpd.hainan.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import com.hzpd.hainan.model.jpush.JpushBean;
import com.hzpd.hainan.model.news.NewsBean;
import com.hzpd.hainan.module.news.activity.NewsHtmlDetailActivity;
import com.hzpd.hainan.utils.LogUtils;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/7/20.
 */

public class JpushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String action = intent.getAction();

        if (bundle == null) {
            return;
        }

        LogUtils.i("onReceive - " + intent.getAction() + ", extras: "
                + printBundle(bundle));
        if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(action)) {
            String title = "";
            String extra = "";
            for (String key : bundle.keySet()) {
                if (key.equals("cn.jpush.android.ALERT")) {
                    title = bundle.getString(key);
                    continue;
                }
                if (key.equals("cn.jpush.android.EXTRA")) {
                    extra = bundle.getString(key);
                    LogUtils.e("extra-----" + extra);
                }
            }

            JpushBean obj = new Gson().fromJson(extra, JpushBean.class);
            if (null == obj) {
                return;
            }
            Intent myIntent = new Intent();
            NewsBean newsBean = new NewsBean();
            newsBean.setNid(obj.getData().getNid());
            myIntent.putExtra("newbean", newsBean);

            if ("1".equals(obj.getAtype())) {
                myIntent.setClass(context, NewsHtmlDetailActivity.class);

            }
            if (myIntent != null) {
                myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myIntent);
            }
        }
    }


    // 打印所有的 intent extra 数据
    private String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        String datd = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        for (String key : bundle.keySet()) {
            String data = bundle.getString(key);
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));

            } else {
                if ("cn.jpush.android.NOTIFICATION_ID".equals(key)) {
                    sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
                } else {
                    sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
                }
            }
        }
        return sb.toString();
    }
}
